package com.komunide.abracadabra;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.view.MotionEvent;
import android.view.View;

import com.larvalabs.svgandroid.SVG;

public class ColoriageView extends View {

	private SVG svg;
	private List<Region> regionsToDraw = new ArrayList<Region>();

	public ColoriageView(Context context, SVG svg) {
		super(context);
		this.svg = svg;
		
		setOnTouchListener(touchListener);
	}
	
	public void draw(Canvas canvas){
		Bitmap bitmap = svg.getBitmap();
		Rect bitmapDimension = new Rect(0,  0, bitmap.getWidth(), bitmap.getHeight());
		Rect canvasDimension = new Rect(0,  0, canvas.getWidth(), canvas.getHeight());
		canvas.drawBitmap(bitmap, bitmapDimension, canvasDimension, null);
		
		Paint paint = new Paint();
		paint.setStrokeWidth(5);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		Random random = new Random();
		for(Region region: regionsToDraw){
			paint.setARGB(100, random.nextInt(255), random.nextInt(255), random.nextInt(255));
			Path path = region.getBoundaryPath();
			RectF bounds = new RectF();
			path.computeBounds(bounds, true);
			canvas.drawRect(bounds, paint);
			canvas.drawPath(path, paint);
		}
	}
	
	
	private OnTouchListener touchListener = new OnTouchListener() {
	    public boolean onTouch(View view, MotionEvent event) {
	      for(Region region: svg.getRegionList()){
	    	  if(region.contains((int)event.getX(), (int)event.getY())){
	    		  if(!regionsToDraw.contains(region))
	    			  regionsToDraw.add(region);
	    		  
	    	  }
	      }
	      view.invalidate();
	      return true;
	    }
	};

}
